<template>
  <el-container style="height: 100%">
    <!--头部区域-->
    <el-header>
      <div>
        <img src="../assets/heima.png">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--页面主题区域-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--侧边栏菜单区域-->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <el-submenu v-for="(itemList) in menuList" :key="itemList.id" :index="itemList.id.toString()">
            <template slot="title">
              <i :class="iconList[itemList.id]"></i>
              <span>{{itemList.authName}}</span>
            </template>
            <el-menu-item
              v-for="(item) in itemList.children"
              :key="item.id"
              v-if="itemList.children"
              :index="item.path"
              @click="saveNavState(item.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{item.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧内容主题-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    name: "Home",
    data(){
      return{
        //左侧菜单数据
        menuList:[],
        iconList:{
          "125":"el-icon-s-custom",
          "103":"el-icon-key",
          "101":"el-icon-s-goods",
          "102":"el-icon-s-order",
          "145":"el-icon-s-marketing"
        },
        //是否折叠
        isCollapse:false,
        //被激活的链接地址
        activePath:""
      }
    },
    created() {
      this.getMenuList();
      this.activePath=window.sessionStorage.getItem("activePath");
    },
    methods:{
      logout(){
        window.sessionStorage.clear();
        this.$router.push("/login");
      },
      async getMenuList(){
        const result=(await this.$http.get("menus")).data;
        if(result.meta.status!==200) return this.$message.error(result.meta.msg);
        this.menuList=result.data;
      },
      //切换菜单折叠与展开
      toggleCollapse(){
        this.isCollapse=!this.isCollapse;
      },
      //保存链接的激活状态
      saveNavState(activePath){
        window.sessionStorage.setItem("activePath",activePath);
        this.activePath=activePath;
      }
    }
  }
</script>

<style scoped lang="less">
  .el-header {
    background-color: #373d41;
    color: #333;
    text-align: center;
    line-height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    font-size: 20px;
    div{
      display: flex;
      align-items: center;
      span{
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background-color: #333744;
    color: #333;
    .toggle-button{
      background-color: #4a5064;
      color: white;
      text-align: center;
      font-size: 10px;
      height: 24px;
      line-height: 24px;
      letter-spacing: 0.2rem;
      cursor: pointer;
    }
  }

  .el-main {
    background-color: #eaedf1;
    color: #333;
  }
  .el-menu{
    border-right: none;
  }
</style>
